#include "gtest/gtest.h"
#include "AsioServerImp.h"
#include "AsioWriteBuffer.h"
#include <iostream>
#include "AsioTimerTask.h"
#include "TimerTask.h"

#include <memory>
#include <unistd.h>

using namespace frame::imp::asio;
TEST(AsioWriteBufferTest, use){
    AsioWriteBuffer awb;

    awb.pushBack("hello");
    awb.pushBack(std::string("hello11111"));
    std::cout << awb << std::endl;

    awb.clear();
    std::cout << "after cleared:  " << awb << std::endl;

}




void fmove(std::shared_ptr<int> && a){
    auto v = std::move(a);
    std::cout << *v << std::endl;
}

TEST(SharedPtrTest, move){
    std::shared_ptr<int> a = std::make_shared<int>(1);
    fmove(std::move(a));
    std::cout << a.use_count() << std::endl;
}


class TimerTask : public frame::imp::ITimerTask {
public:
    TimerTask(frame::imp::ServerImp * serverImp)
    : serverImp(serverImp)
    , count(0)
    {
    }

    ~TimerTask() override {};

    void handle(frame::imp::Timer * timer) override{
        ++count;
        std::cout << "timer id:" << getTimerId()  << ", count:" << count<< std::endl;
        if(count == 2){
            serverImp->removeTimerTask(getTimerId());
        }
    }
private:
    frame::imp::ServerImp * serverImp;
    int count;
};

TEST(AsioServerImpTest, use){

    auto serverImp = std::shared_ptr<frame::imp::ServerImp>(new AsioServerImp(1234));
    //serverImp->setHandler(new DefaultHandlerImp);

    TimerTask tt(serverImp.get());
    serverImp->addTimerTask(&tt, 100);
    serverImp->start();

    std::thread t2(
        [serverImp](){
            serverImp->run();
        }
    );

    sleep(1);

    serverImp->stop();
    t2.join();
}

#include "HeartBeatServerImp.h"

TEST(HeartBeatServer, use){
    /*
    auto serverImp = std::make_shared<frame::imp::HeartBeatServerImp>(new AsioServerImp(1235), 5000, 3000);
    serverImp->start();
    serverImp->run();
    serverImp->stop();

//    auto factory = new AsioFactory();
//    auto * serverImp = factory->createServerImp();
//    factory->destroyServerImp(serverImp);
//
//    auto * serverChannelImp = factory->createAsioServerChannelImp(serverImp);
//    factory->destroyAsioServerChannelImp(serverChannelImp);
//
//    auto * forwardImp = factory->createForwardImp();
//    factory->destroyForwardImp(forwardImp);
    */
}
